import {
  Entity,
  Column,
  CreateDateColumn,
  UpdateDateColumn,
  PrimaryColumn,
  OneToOne,
  JoinColumn
} from "typeorm";
import { BblPrizeGroup } from "./prize.group.entity";

/**
 * 抽奖活动批次管理表
 */
@Entity("BBL_PRIZE_ACTIVITY")
export class BblPrizeActivity {
  /** 活动批次主键ID（业务自定义） */
  @PrimaryColumn({
    name: "ID",
    type: "varchar",
    length: 50,
    comment: "活动批次主键ID（业务自定义）"
  })
  id: string;

  /** 活动批次名称 */
  @Column({
    name: "BATCH_NAME",
    type: "varchar",
    length: 100,
    nullable: false,
    comment: "活动批次名称"
  })
  batchName: string;

  /** 活动批次描述（支持富文本） */
  @Column({
    name: "BATCH_DESC",
    type: "text",
    nullable: true,
    comment: "活动批次描述"
  })
  batchDesc: string;

  /** 活动开始时间（自动记录首次创建时间） */
  @Column({
    name: "START_TIME",
    type: "timestamp",
    precision: 6,
    nullable: true,
    default: () => "CURRENT_TIMESTAMP(6)",
    comment: "活动开始时间"
  })
  startTime: Date;

  /** 活动结束时间（可手动设置） */
  @Column({
    name: "END_TIME",
    type: "timestamp",
    precision: 6,
    nullable: true,
    default: () => "CURRENT_TIMESTAMP(6)",
    comment: "活动结束时间"
  })
  endTime: Date;

  /** 总抽奖码数量（活动可发放总数） */
  @Column({
    name: "TOTAL_CODES",
    type: "int",
    nullable: false,
    comment: "总抽奖码数量"
  })
  totalCodes: number;

  /** 已使用抽奖码数量（实时统计） */
  @Column({
    name: "USED_CODES",
    type: "int",
    default: 0,
    comment: "已使用抽奖码数量"
  })
  usedCodes: number;

  @OneToOne(() => BblPrizeGroup, { createForeignKeyConstraints: false })
  @JoinColumn({ name: "GROUP_ID" })
  prizeGroup: BblPrizeGroup;

  /**
   * 批次状态：
   * 1-未开始
   * 2-进行中
   * 3-已结束
   * 4-已取消
   */
  @Column({
    name: "STATUS",
    type: "int",
    nullable: false,
    comment: "批次状态（1-未开始，2-进行中，3-已结束，4-已取消）"
  })
  status: number;

  /** 创建时间（自动记录） */
  @CreateDateColumn({
    name: "CREATED_AT",
    type: "timestamp",
    precision: 6,
    nullable: true,
    default: () => "CURRENT_TIMESTAMP(6)",
    comment: "创建时间"
  })
  createdAt: Date;

  /** 最后更新时间（自动更新） */
  @UpdateDateColumn({
    name: "UPDATED_AT",
    type: "timestamp",
    precision: 6,
    nullable: true,
    default: () => "CURRENT_TIMESTAMP(6)",
    onUpdate: "CURRENT_TIMESTAMP(6)",
    comment: "更新时间"
  })
  updatedAt: Date;

  /** 创建人ID（系统操作员） */
  @Column({
    name: "CREATED_BY",
    type: "varchar",
    length: 50,
    nullable: true,
    comment: "创建人ID"
  })
  createdBy: string;

  /** 最后更新人ID（系统操作员） */
  @Column({
    name: "UPDATED_BY",
    type: "varchar",
    length: 50,
    nullable: true,
    comment: "更新人ID"
  })
  updatedBy: string;
}
